const express  = require ('express');
const pool = require('../pool');

const router = express.Router();

router.get('/search',async(req,res,next)=>{
    let name = '%' + req.query.name + '%';
    let page = (req.query.page - 1) * 18
    let pageCount = 18;
    pool.query(`select d_id,d_names,look,down,src,museum.m_names,person.p_names,person.dynasty from draw,museum,person where w_id = museum.m_id and w_id = person.p_id and d_names like ? limit ${page},${pageCount}`,[name],async(err,r)=>{
        if(err){
            return next(err)
        }
        pool.query('select count(d_names) from draw where d_names like ?',[name],(err,resp)=>{
            if(err){
                return next(err)
            }
            let pageCount = resp[0]['count(d_names)']
            res.send({code:200,data:{result:r,pageCount}})
        })
       
    });
})

router.get('/querAll',async(req,res,next)=>{
    let page = (req.query.page - 1) * 18
    let pageCount = 18;
    console.log(page)
    pool.query(`select d_names,src,look,down,person.p_names,person.dynasty from draw,person where did=person.p_id limit ${page},${pageCount}`,(err,r)=>{
        if(err){
            return next(err)
        }
        pool.query('select count(*) from draw',(err,resp)=>{
            let pageSize = Math.ceil(resp[0]['count(*)']/pageCount)
            if(r.length!=0){
                res.send({code:200,data:{result:r,pageSize}})
            }else{
                res.send({code:400,data:'没有更多了'})
            }
        })
        
    })
})

router.post('/add',(req,res,next)=>{
    let name= req.body.p_name;//作者名
    
    let src = req.body.imgSrc;//添加的图片路径
    console.log(req.body.imgSrc)
    let person = Math.floor(Math.random()*275); //画作关联的作者id
    let museun = Math.floor(Math.random()*21); //画作关联的博物馆id
    pool.query(`insert into draw values(null,?,default,default,?,${person},${museun});`,[req.body.p_name,req.body.imgSrc],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'添加成功'})
    })
})

router.post('/dele',async(req,res,next)=>{
    let id = req.body.d_id //画作id
    pool.query(`delete from draw where d_id=${id}`,(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'删除成功'})
    })
})

router.post('/update',async(req,res,next)=>{
    let d_id = req.body.d_id //需要修改的画作id
    let name = req.body.name;//作品名称
    let src = req.body.imgSrc;//图片地址
    // let person = req.body.person;//作者名字

    console.log(req.body.name)
    pool.query(`update draw set d_names=?,src=? where d_id=?`,[name,src,d_id],(err,r)=>{
        if(err){
            return next(err)
        }
    })
})


//查询所有浏览量
router.get("/queryLook",async (req,res,next)=>{
      let sql="select sum(look) as sumld from draw"
      pool.query(sql,(err,r)=>{
         if(err){
          return next(err)
        }
         res.send({code:200,data:r})
     })
})
        //查询所有下载量
router.get("/queryDown",async (req,res,next)=>{
   let sql="select sum(down) as sumdd from draw"
      pool.query(sql,(err,r)=>{
         if(err){
           return next(err)
        }
        res.send({code:200,data:r})
    })
})



module.exports = router;